package com.sky.aspect;

import com.sky.context.BaseContext;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.properties.JwtProperties;
import com.sky.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.time.LocalDateTime;

@Aspect
@Slf4j
@Component
public class AutoLogAspect {

    @Pointcut("execution(* com.sky.service.*.*(..)) && @annotation(com.sky.annotation.Log)")
    public void logPointCut(){}

    @Around("logPointCut()")
    public Object log(ProceedingJoinPoint joinPoint) {
        log.info("记录日志");

        //操作人id
        Long currentId = BaseContext.getCurrentId();

        //操作时间
        long begin = System.currentTimeMillis();

        Object returnValue = null;
        try {
            returnValue = joinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }

        long end = System.currentTimeMillis();

        //操作方法名
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        String name = signature.getMethod().getName();

        log.info("操作人id:{},操作方法名:{},操作耗时:{}",currentId, name, (end - begin));

       return returnValue;
    }
}
